<template>
<!-- 展示excel模板并提供下载 -->
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="信息描述">
        <el-tag type="info">excel模板</el-tag>
        <el-tag>
          <i class="el-icon-download" />
          <a :href="'https://edu-9-22.oss-cn-shanghai.aliyuncs.com/2021/09/23/%E8%AF%BE%E7%A8%8B%E6%A8%A1%E6%9D%BF.xlsx'">点击下载模版</a>
        </el-tag>
      </el-form-item>
<!-- 上传excel模板的上传组件 -->
      <el-form-item label="选择Excel">
          <!-- ref-相当于一个标识、auto-upload:自动上传(不用点按钮选择文件就上传)、on-success/error:成功/失败回调方法
              disabled-点击1次后按钮不能点第2次、limit:限制文件上传数量、name:类型、accept:文件格式
           -->
        <el-upload
          ref="upload"   
          :auto-upload="false"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :disabled="importBtnDisabled"
          :limit="1"
          :action="BASE_API + '/eduservice/subject/addSubject'"
          name="file"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        >
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button
            :loading="loading"
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload"
          >上传到服务器</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      BASE_API: process.env.BASE_API, // 接口API地址
      importBtnDisabled: false, // 按钮是否禁用,
      loading: false //加载效果
    };
  },
  created(){},
  methods:{
      //点击按钮上传文件到接口里
      submitUpload(){
          this.importBtnDisabled=true
          this.loading=true
          this.$refs.upload.submit() //类似于原生JS:document.getElementById('upload').submit()
      },
      //上传成功的方法
      fileUploadSuccess(response){
          this.loading=false;
          //提示信息
          this.$message({
              type:'success',
              message:'添加课程分类成功!'
          })
          //路由跳转到列表页面
          this.$router.push({path:'/subject/list'})
      },
      //上传失败的方法
      fileUploadError(){
          this.loading=false
          //提示信息
         this.$message({
              type:'error',
              message:'添加课程分类失败!'
          })
      }
  }
};
</script>
